/**
 * @author roua_troua
 * 
 */
package q10.Visitor;

import q10.Arbori.Nod;
import q10.Functii.FunctiiElementare.Constanta;
import q10.Functii.FunctiiElementare.Necunoscuta;
import q10.Functii.FunctiiElementare.Putere;
import q10.Functii.FunctiiTrigonometrice.*;
import q10.Operatori.*;

public class CalculVisitor extends Visitor{
	protected String rezultat;
	protected double calcul;
	//protected ToStringVisitor tsv=new ToStringVisitor();

	/**
	 * Constructor 
	 */
	public CalculVisitor(){
		this.rezultat="";
		this.calcul=0;
	}
	
	/**
	 * Constructor
	 * @param calcul
	 */
	public CalculVisitor(double calcul){
		this.calcul=calcul;
		this.rezultat=Double.toString(calcul);
	}
	
	
	/**
	 * getRezultat
	 * @param
	 * @return rezultat String
	 */
	public String getRezultat() {
		return rezultat;
	}
	
	/**
	 * setRezultat
	 * @param rezultat
	 * @return
	 */
	public void setRezultat(String rezultat) {
		this.rezultat = rezultat;
	}
	
	/**
	 * getCalcul
	 * @return calcul double
	 */
	public double getCalcul() {
		return calcul;
	}

	/**
	 * setCalcul
	 * @param calcul
	 */
	public void setCalcul(double calcul) {
		this.calcul = calcul;
	}
	
	/**
	 * visit constanta
	 * @param Constanta
	 * @return
	 */
	public void visit (Constanta cn)
	{
		this.calcul= cn.getCt();
	}

	/**
	 * visit necunoscuta
	 * @param Necunoscuta
	 * @return
	 */
	public void visit (Necunoscuta n)
	{
		this.calcul=n.getX();
	}
	
	/**
	 * visit sinus
	 * @param Sin
	 * @return
	 */
	public void visit(Sin s){
		if(s.getfStang() != null){
			visit(s.getfStang());
			double fs = this.calcul;
			this.calcul = Math.sin(fs);
		}
	}
	
	/**
	 * visit cosinus
	 * @param Cos
	 * @return
	 */
	public void visit(Cos c){
		if(c.getfStang() != null){
			visit(c.getfStang());
			double fs=this.calcul;
			this.calcul=Math.cos(fs);
		}
	}
	
	/**
	 * visit cat
	 * @param Cat
	 * @return 
	 */
	public void visit(Cat c) {
		if((c.getfStang() != null) && (c.getfDrept() != null)){
			visit(c.getfStang());
			double fs = this.calcul;
			visit(c.getfDrept());
			double fd= this.calcul;
			if(fd != 0)
				this.calcul = fs/fd;
			else
				this.calcul = 0;
		}
	}

	/**
	 * visit minus
	 * @param Minus
	 * @return
	 */
	public void visit(Minus m) {
		if((m.getfStang() != null) && (m.getfDrept() != null)){
			visit(m.getfStang());
			double fs=this.calcul;
			visit(m.getfDrept());
			double fd= this.calcul;
			this.calcul= fs-fd;
		}
	}

	/**
	 * visit plus
	 * @param Plus
	 * @return
	 */
	public void visit(Plus p) {
		if((p.getfStang()!=null)&&(p.getfDrept()!=null)){
			visit(p.getfStang());
			double fs = this.calcul;
			visit(p.getfDrept());
			double fd = this.calcul;
			this.calcul = fs + fd;
		}
	}

	/**
	 * visit produs
	 * @param Produs
	 * @return
	 */
	public void visit(Produs p) {
		if((p.getfStang()!=null)&&(p.getfDrept()!=null)){
			visit(p.getfStang());
			double fs=this.calcul;
			visit(p.getfDrept());
			double fd= this.calcul;
			this.calcul= fs*fd;
		}
	}

	/**
	 * visit putere
	 * @param Putere
	 * @return
	 */
	public void visit(Putere p) {
		if((p.getfStang()!=null)&&(p.getfDrept()!=null)){
			visit(p.getfStang());
			double fs=this.calcul;
			visit(p.getfDrept());
			double fd= this.calcul;
			this.calcul= Math.pow(fs, fd);
		}
	}
	
	/**
	 * Neimplementata
	 */
	public void visit(Tg tg){
		
	}
	
	/**
	 * Neimplementata
	 */
	public void visit(Ctg ctg){
		
	}


	/**
	 * visit Nod
	 * @param Nod
	 * @return
	 */
	public void visit(Nod n) {
		if(n instanceof Plus)
			visit((Plus)n);
			else if (n instanceof Constanta)
				visit((Constanta)n);
				else if (n instanceof Necunoscuta)
					visit((Necunoscuta)n);
					else if (n instanceof Minus)
						visit((Minus)n);
						else if (n instanceof Cat)
							visit((Cat)n);
							else if (n instanceof Plus)
								visit((Plus)n);
								else if (n instanceof Produs)
									visit((Produs)n);
									else if (n instanceof Putere)
										visit((Putere)n);
										else if (n instanceof Cos)
											visit((Cos)n);		
	}
}
